<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>表单页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/layui/layui.js"></script>
    <script src="/res/js/tools.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户信息维护</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="example" id="feiziForm">
    <input type="hidden" name="id" id="id"><!-- 加一个隐藏主键 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">人名</label>
            <div class="layui-input-inline">
                <input type="text" id="pname" name="pname" readonly class="layui-input">
                <input type="hidden" name="pid" id="pid">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">妃子</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>

    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline">
                <input type="radio" name="sex" value="男" title="男" checked="">
                <input type="radio" name="sex" value="女" title="女">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">生日</label>
            <div class="layui-input-inline">
                <input type="text" name="birthday_" id="birthday_" lay-verify="required" autocomplete="off" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>

    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">照骗</label>
            <div class="layui-input-inline">
                <input type="file" name="photo_">
            </div>
        </div>


    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="tijiao">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>

</form>
<script>
    layui.use(['form', 'layedit', 'laydate','jquery','tableSelect'], function() {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        var tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#pname',	//定义输入框input对象 必填
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url:'/person/findAll.action',
                cols: [[ //表头
                    {type: 'radio'}
                    ,{field: 'id', title: 'ID'}
                    ,{field: 'name', title: '用户名'}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                console.info(data);
                $("#pid").val(data.data[0].id);
                $("#pname").val(data.data[0].name);


            }
        })

        laydate.render({
            elem:'#birthday_'});
        //创建一个编辑器
        //var editIndex = layedit.build('userRemark');

        //监听提交
        form.on('submit(tijiao)', function(data){
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })

            //利用ajax向后台提交数据
            let url = "/feizi/addOrUpdate.action";
            let data_ = new FormData(document.getElementById("feiziForm"));
            console.info(data_);

            $.ajax({
                url:url,
                data:data_,
                type: 'POST',   //上传文件必须使用post
                dataType:'json',
                processData: false, // 告诉jquery要传输data对象
                contentType: false,   // 告诉jquery不需要增加请求头对于contentType的设置
                success: function (resp) { //如果上传成功,就执行

                    if(resp.state==1){
                        //注意：parent 是 JS 自带的全局对象，可用于操作父页面
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                        //父页面刷新
                        parent.table.reload('dataListTable');

                        //父页面提示信息
                        parent.layer.msg(resp.msg);

                    }else{
                        layer.msg(resp.msg);
                    }

                }
                ,error:function (arg) { //如果上传失败,就执行
                    console.log(arg)
                }
            })


            return false;
        });

        $("#close_btn").on("click",function () {
            layerClose();
        });

        /*
        * 当点击编辑按钮时,会传入一个id到form.html页面
        * */
        let id = getQueryString("id");
        //如果id有值
        if(id){
            let url = "/feizi/showUpdate.action";
            let data_ = {id:id};
            $.post(url,data_,function (resp) {
                if(resp.state==1){
                    //$("#userId").val(resp.data.userId);
                    //$("#userName").val(resp.data.userName);

                    form.val('example', resp.data);
                    //重新渲染
                    form.render();

                }
            },"json");
        }


    });



</script>
</body>
</html>